#include<SFML/Graphics.hpp>
#include <vector>

// 窗口大小和贪吃蛇方格大小（像素）
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int GRID_SIZE = 20;

// 贪吃蛇方向
enum Direction { Up, Down, Left, Right };

// 贪吃蛇的一个身体部分
struct SnakeSegment {
    int x, y;
    SnakeSegment(int x, int y) : x(x), y(y) {}
};

int main() {
    sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Snake Game");

    // 设置窗口不接受重复按键
    window.setKeyRepeatEnabled(false);

    // 创建一个贪吃蛇头部并初始化
    SnakeSegment head(0, 0);
    Direction direction = Right;

    // 创建一个存储贪吃蛇身体部分的向量
    std::vector<SnakeSegment> segments;
    segments.push_back(head);

    // 创建一个食物并随机放置
    sf::RectangleShape food(sf::Vector2f(GRID_SIZE, GRID_SIZE));
    food.setFillColor(sf::Color::Red);
    int foodX = rand() % (WINDOW_WIDTH / GRID_SIZE);
    int foodY = rand() % (WINDOW_HEIGHT / GRID_SIZE);
    food.setPosition(foodX * GRID_SIZE, foodY * GRID_SIZE);

    // 游戏循环
    while (window.isOpen()) {
        // 处理事件
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            } else if (event.type == sf::Event::KeyPressed) {
                // 处理按键事件，改变贪吃蛇的方向
                switch (event.key.code) {
                    case sf::Keyboard::Up:
                        if (direction != Down) direction = Up;
                        break;
                    case sf::Keyboard::Down:
                        if (direction != Up) direction = Down;
                        break;
                    case sf::Keyboard::Left:
                        if (direction != Right) direction = Left;
                        break;
                    case sf::Keyboard::Right:
                        if (direction != Left) direction = Right;
                        break;
                }
            }
        }

        // 更新贪吃蛇的位置
        switch (direction) {
            case Up:
                head.y -= GRID_SIZE;
                break;
            case Down:
                head.y += GRID_SIZE;
                break;
            case Left:
                head.x -= GRID_SIZE;
                break;
            case Right:
                head.x += GRID_SIZE;
                break;
        }

        // 检测是否吃到食物，如果吃到则添加身体部分并重新生成食物
        if (head.x == foodX * GRID_SIZE && head.y == foodY * GRID_SIZE) {
            segments.push_back(SnakeSegment(0, 0));
            foodX = rand() % (WINDOW_WIDTH / GRID_SIZE);
            foodY = rand() % (WINDOW_HEIGHT / GRID_SIZE);
            food.setPosition(foodX * GRID_SIZE, foodY * GRID_SIZE);
        }

        // 移动身体部分，每个




    // 移动身体部分，每个身体部分的位置都设置为前一个身体部分的位置
    for (int i = segments.size() - 1; i > 0; --i) {
        segments[i].x = segments[i-1].x;
        segments[i].y = segments[i-1].y;
    }

    // 设置贪吃蛇头部的位置
    segments[0].x = head.x;
    segments[0].y = head.y;

    // 检测是否撞到边界或者撞到自己，如果是则游戏结束
    if (head.x < 0 || head.x >= WINDOW_WIDTH || head.y < 0 || head.y >= WINDOW_HEIGHT) {
        window.close();
    }
    for (int i = 1; i < segments.size(); ++i) {
        if (head.x == segments[i].x && head.y == segments[i].y) {
            window.close();
        }
    }

    // 绘制场景
    window.clear();

    window.draw(food);

    for (auto segment : segments) {
        sf::RectangleShape rect(sf::Vector2f(GRID_SIZE, GRID_SIZE));
        rect.setFillColor(sf::Color::Green);
        rect.setPosition(segment.x, segment.y);
        window.draw(rect);
    }

    window.display();
}

return 0;
}